//
//  AppDelegate.m
//  UI02_UILabel_UITextField_UIButton_UIImageView
//
//  Created by Scott on 16/3/10.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "ColorView.h"
#import "MyLabel.h"
#import "MyTextField.h"
#import "MyButton.h"
#import "MyImageView.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface AppDelegate () <UITextFieldDelegate>

@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [super dealloc];
}




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    self.window.backgroundColor = [UIColor cyanColor];
    [self.window makeKeyAndVisible];
    
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    
//    [self homework];
    
    [self createLabel];
    [self createTextField];
    [self createButton];
    [self createImageView];
    
    
    [vc release];
    [_window release];
    
    return YES;
}

#pragma mark - 前一天作业
- (void)homework {
    
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 5; j++) {
            
            ColorView *view = [[ColorView alloc] initWithFrame:CGRectMake(WIDTH / 5 * j, HEIGHT / 8 * i, WIDTH / 5, HEIGHT / 8)];
            [self.window addSubview:view];
            [view release];
        }
    }
    
}

#pragma mark - 知识点1: UILabel
- (void)createLabel {
    
    // 创建对象
    MyLabel *labelOfUsername = [[MyLabel alloc] initWithFrame:CGRectMake(20, 50, 100, 40) text:@"用户名:"];
    [self.window addSubview:labelOfUsername];
    [labelOfUsername release];
    
    
    // 密码
    MyLabel *labelOfPasswd = [[MyLabel alloc] initWithFrame:CGRectMake(20, 120, 100, 40) text:@"密 码:"];
    [self.window addSubview:labelOfPasswd];
    [labelOfPasswd release];
    
}

#pragma mark - 知识点2: UITextField
- (void)createTextField {
    
    // 用户名
    MyTextField *textFieldOfUsername = [[MyTextField alloc] initWithFrame:CGRectMake(150, 50, 200, 40) placeHolder:@"请输入用户名"];
    
    [self.window addSubview:textFieldOfUsername];
    
    [textFieldOfUsername release];
    
    // 指定代理人, self此时指appDelegate对象本身.
    textFieldOfUsername.delegate = self;
    
    textFieldOfUsername.tag = 100;
    
    // 密码
    MyTextField *textFieldOfPasswd = [[MyTextField alloc] initWithFrame:CGRectMake(150, 120, 200, 40) placeHolder:@"请输入密码"];
    
    [self.window addSubview:textFieldOfPasswd];
    
    [textFieldOfPasswd release];
    
    // 密文输入
    textFieldOfPasswd.secureTextEntry = YES;
    
    // 键盘类型
    textFieldOfPasswd.keyboardType = UIKeyboardTypeDefault;
    
    // 回车键style
    textFieldOfPasswd.returnKeyType = UIReturnKeyJoin;
    
    textFieldOfPasswd.delegate = self;
    
    textFieldOfPasswd.tag = 200;
}

#pragma mark - 知识点3: UITextField协议方法
/** 将要开始编辑, 调用此方法. */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    // 获取对象
    MyTextField *user = [self.window viewWithTag:100];
    
    MyTextField *passwd = [self.window viewWithTag:200];
    

    if (textField.tag == 200 && [user.text isEqualToString:@""]) {
        return NO;
        
    } else {
        return YES;
    }
    
    

    
    
    
    return YES;

}

/** 当点击键盘上的return按键. */
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    // 隐藏键盘
    [textField resignFirstResponder];
    
    return YES;

}

/** 文本内容发生变化, 调用此方法. */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSLog(@"%ld, %ld", range.location, range.length);
    
    // 限制输入数字 1.
    if ([string isEqualToString:@"1"]) {
        return NO;
    
        // 限制输入6个字符
    } else if (range.location > 5) {
        return NO;
        
    } else {
        return YES;
    }

}

#pragma mark - 知识点4: UIButton
- (void)createButton{
    
    // 创建对象
    MyButton *buttonOfOK = [MyButton buttonWithType:UIButtonTypeSystem];
    [self.window addSubview:buttonOfOK];
    
    buttonOfOK.frame = CGRectMake(20, 200, 150, 40);
    
    buttonOfOK.backgroundColor = [UIColor redColor];
    
    // 标题
    [buttonOfOK setTitle:@"确定" forState:UIControlStateNormal];
    
    // 字体
    buttonOfOK.titleLabel.font = [UIFont systemFontOfSize:25];
    
    // 添加事件
    [buttonOfOK addTarget:self action:@selector(handleOK:) forControlEvents:UIControlEventTouchUpInside];
    
    MyButton *buttonOFCancel = [MyButton buttonWithType:UIButtonTypeCustom];
    
    [self.window addSubview:buttonOFCancel];
    
    buttonOFCancel.frame = CGRectMake(200, 200, 150, 40);
    buttonOFCancel.backgroundColor = [UIColor yellowColor];
    
    // 设置图片
//    [buttonOFCancel setBackgroundImage:[UIImage imageNamed:@"iconfont-gouxuan.png"] forState:UIControlStateNormal];
    
    [buttonOFCancel setImage:[[UIImage imageNamed:@"iconfont-gouxuan.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
  
    [buttonOFCancel setTitle:@"取消" forState:UIControlStateNormal];
    
    
}

- (void)handleOK:(UIButton *)button {
    
    // 回收键盘
    [self.window endEditing:YES];

}

#pragma mark - 知识点5: UIImageView
- (void)createImageView {
    
    // 创建对象
    MyImageView *imageView = [[MyImageView alloc] initWithImage:[UIImage imageNamed:@"4.jpg"]];
    
    imageView.frame = CGRectMake(100, 300, 200, 200);
    
    [self.window addSubview:imageView];
    
    [imageView release];
    
}






#pragma mark - other
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
